﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Attendance.AttendanceCheckInFlow;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceCheckInFlow
{
    public class AttendanceCheckInFlowData : IAttendanceCheckInFlowData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT
                                        CheckInRecordId,
                                        AdminId,
                                        CheckInFlowId,
                                        AdminName,
                                        CheckInTime,
                                        Location,
                                        Longitude,
                                        Latitude,
                                        Remark,
                                        WifiName,
                                        MAC,
                                        CreateTime ,BaseCheckInTime
                                        FROM
                                        OA_AttendanceCheckInFlow Where 1=1 ";

        public AttendanceCheckInFlowData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        /// <summary>
        /// 获取指定打卡记录下的流水
        /// </summary>
        /// <param name="checkInRecordId">打卡记录Id</param>
        /// <returns></returns>

        public async Task<IEnumerable<AttendanceCheckInFlowEntity>> GetListByCheckInRecordIdAsync(long checkInRecordId)
        {
            string sql = string.Format("{0} AND CheckInRecordId=?CheckInRecordId Order By CreateTime Desc", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CheckInRecordId", checkInRecordId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<AttendanceCheckInFlowEntity>(sql, parameters);
        }

        /// <summary>
        /// 新增一条考勤打卡记录流水
        /// </summary>
        /// <param name="entitys">打卡信息</param>
        /// <returns></returns>
        public async Task<AttendanceCheckInFlowEntity> InserAsync(AttendanceCheckInFlowEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_AttendanceCheckInFlow", new string[] { "CheckInRecordId", "AdminId", "AdminName", "CheckInTime", "Location", "Longitude", "Latitude", "Remark", "WifiName", "MAC", "BaseCheckInTime" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CheckInRecordId", entity.CheckInRecordId, DbType.Int64);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?AdminName", entity.AdminName, DbType.AnsiString, size: 30);
            parameters.Add("?CheckInTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?Location", entity.Location, DbType.AnsiString, size: 100);
            parameters.Add("?Longitude", entity.Longitude, DbType.Double);
            parameters.Add("?Latitude", entity.Latitude, DbType.Double);
            parameters.Add("?WifiName", entity.WifiName, DbType.AnsiString, size: 50);
            parameters.Add("?MAC", entity.MAC, DbType.AnsiString, size: 50);
            parameters.Add("?Remark", entity.Remark, DbType.AnsiString);
            parameters.Add("?BaseCheckInTime", entity.BaseCheckInTime, DbType.AnsiString, size: 50);

            string querySql = string.Format(@"{0}{1} AND CheckInFlowId=LAST_INSERT_ID();", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceCheckInFlowEntity>(querySql, parameters, databaseContext.Transaction);
        }
    }
}
